//
//  ActivityDetailViewController.m
//  Contacts
//
//  Created by 李 雷川 on 14-7-19.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ActivityDetailViewController.h"
#import "CommentCell.h"
#import "ActivityDetailCell.h"
#import "CommentView.h"

@interface ActivityDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>{
    IBOutlet UITableView  *detailTableView;
    IBOutlet CommentView  *commentView;
}
@property(nonatomic,strong)    IBOutlet UITableView  *detailTableView;
@property(nonatomic,strong)IBOutlet CommentView  *commentView;
@end

@implementation ActivityDetailViewController
@synthesize detailTableView,commentView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        
    }
    return self;
}

-(void)awakeFromNib{


}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"活动详情";
    [self setupSubViews];
}


-(void)setupSubViews{
    CGRect tableFrame = CGRectMake(0,64,CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame) - 118);

    detailTableView = [[UITableView alloc]initWithFrame:tableFrame];
    [self.view addSubview:detailTableView];
    detailTableView.delegate = self;
    detailTableView.dataSource = self;
    
    
    
    CGRect commentFrame = CGRectMake(0,CGRectGetMaxY(tableFrame),CGRectGetWidth(self.view.frame),54);;
    commentView = [[CommentView alloc]initWithFrame:commentFrame];
    [commentView.commentField setDelegate:self];
    [self.view addSubview:commentView];
}

-(void)viewWillAppear:(BOOL)animated{

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)initWithActivity:(Activity *)_activity{
    activity = _activity;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,200,20)];
        label.numberOfLines = 0;
        label.font =[UIFont systemFontOfSize:16];
        label.text = activity.subject;
        CGSize subjectSize =   [label sizeThatFits:CGSizeMake(200, 500)];
        label.text = activity.content;
        label.font =[UIFont systemFontOfSize:14];
        CGSize detailSize =   [label sizeThatFits:CGSizeMake(200, 500)];
        
        float defalutHeight =subjectSize.height + detailSize.height + 115;
        return defalutHeight;
    }
    return 64;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 30;
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return activity.commentArray.count;
            break;
            
        default:
            break;
    }
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        
        return [NSString stringWithFormat:@"共%lud条评论",activity.commentArray.count];
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *  cell;
    switch (indexPath.section) {
        case 0:
        {
            cell = (ActivityDetailCell *)[tableView dequeueReusableCellWithIdentifier:@"ActivityDetailCell"];
            if (!cell) {
                cell = [[ActivityDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ActivityDetailCell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            ActivityDetailCell *detaillCell =(ActivityDetailCell *) cell;
            [detaillCell initWithActivity:activity];
            return cell;
        }
            break;
        case 1:
        {
            cell = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
            if (!cell) {
                cell = [[CommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CommentCell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            CommentCell *commentCell =(CommentCell *) cell;
            Comment *comment = activity.commentArray[indexPath.row];
            [commentCell initWithComent:comment];
            return cell;
        }
            break;
            
        default:
            break;
    }
  
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    CGRect frame = commentView.frame;
    frame.origin.y = 0;
    commentView.frame = frame;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    CGRect frame = commentView.frame;
    frame.origin.y = CGRectGetMaxY(detailTableView.frame);
    commentView.frame = frame;
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
